package org.jeecg.modules.bus.controller;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.bus.entity.BusTenderApplicationProInfTrack;
import org.jeecg.modules.bus.entity.BusTenderResultVO;
import org.jeecg.modules.bus.entity.TenderResult;
import org.jeecg.modules.bus.entity.*;
import org.jeecg.modules.bus.service.IBusTenderApplicationProInfTrackService;
import org.jeecg.modules.bus.service.IBusTenderRecordService;
import org.jeecg.modules.bus.service.ITenderResultService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 商务管理（投标结果）
 * @Author: jeecg-boot
 * @Date:   2022-11-15
 * @Version: V1.0
 */
@Api(tags="商务管理（投标结果）")
@RestController
@RequestMapping("/bus/tenderResult")
@Slf4j
public class TenderResultController extends JeecgController<TenderResult, ITenderResultService> {
	@Autowired
	private ITenderResultService tenderResultService;
	@Autowired
	private IBusTenderApplicationProInfTrackService busTenderApplicationProInfTrackService;
	@Autowired
	private IBusTenderRecordService busTenderRecordService;

	/**
	 * 分页列表查询
	 *
	 * @param tenderResult
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-分页列表查询")
	@ApiOperation(value="商务管理（投标结果）-分页列表查询", notes="商务管理（投标结果）-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TenderResult tenderResult,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TenderResult> queryWrapper = QueryGenerator.initQueryWrapper(tenderResult, req.getParameterMap());
		Page<TenderResult> page = new Page<TenderResult>(pageNo, pageSize);
		IPage<TenderResult> pageList = tenderResultService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param busTenderResultVO
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-添加")
	@ApiOperation(value="商务管理（投标结果）-添加", notes="商务管理（投标结果）-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody BusTenderResultVO busTenderResultVO) {
		tenderResultService.save(busTenderResultVO.getTenderResult());
		// 1.根据开标记录id查询到投标申请id;
		String tenderApplicationId = busTenderRecordService.queryByRecordId(busTenderResultVO.getTenderResult().getBidRecordId());
		// 2.根据投标申请id 得到项目清单
		QueryWrapper<BusTenderApplicationProInfTrack> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("tender_application_id",tenderApplicationId);
		List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList_bef = busTenderApplicationProInfTrackService.list(queryWrapper);
		List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList = busTenderResultVO.getBusTenderApplicationProInfTrackList();
		if(busTenderApplicationProInfTrackList_bef.size()!=busTenderApplicationProInfTrackList.size()) {
			return Result.error("投标结果项目清单和投标申请项目清单不一致！");
		}
		//	3.将前端传过来的二次金额更新到项目清单里。
		for (int i =0;i< busTenderApplicationProInfTrackList_bef.size();i++){
			BusTenderApplicationProInfTrack busTenderApplicationProInfTrack_bef = busTenderApplicationProInfTrackList_bef.get(i);
			BusTenderApplicationProInfTrack busTenderApplicationProInfTrack = busTenderApplicationProInfTrackList.get(i);
			busTenderApplicationProInfTrack_bef.setSecondQuotationPrice(busTenderApplicationProInfTrack.getSecondQuotationPrice());
			busTenderApplicationProInfTrackService.updateById(busTenderApplicationProInfTrack_bef);
		}

		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tenderResult
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-编辑")
	@ApiOperation(value="商务管理（投标结果）-编辑", notes="商务管理（投标结果）-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TenderResult tenderResult) {
		tenderResultService.updateById(tenderResult);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-通过id删除")
	@ApiOperation(value="商务管理（投标结果）-通过id删除", notes="商务管理（投标结果）-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tenderResultService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-批量删除")
	@ApiOperation(value="商务管理（投标结果）-批量删除", notes="商务管理（投标结果）-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tenderResultService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param recordId
	 * @return
	 */
	@AutoLog(value = "商务管理（投标结果）-通过recordId查询")
	@ApiOperation(value="商务管理（投标结果）-通过recordId查询", notes="商务管理（投标结果）-通过recordId查询")
	@GetMapping(value = "/queryByRecordId")
	public Result<?> queryByRecordId(@RequestParam(name="recordId",required=true) String recordId) {
		// 1.根据开标记录id查询到投标申请id;
		String tenderApplicationId = busTenderRecordService.queryByRecordId(recordId);
		// 2.根据投标申请id 得到项目清单
		QueryWrapper<BusTenderApplicationProInfTrack> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("tender_application_id",tenderApplicationId);
		List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList = busTenderApplicationProInfTrackService.list(queryWrapper);
		return Result.OK(busTenderApplicationProInfTrackList);
	}

	 /**
	  * 通过id查询
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "商务管理（投标结果）-通过id查询")
	 @ApiOperation(value="商务管理（投标结果）-通过id查询", notes="商务管理（投标结果）-通过id查询")
	 @GetMapping(value = "/queryById")
	 public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		 TenderResult tenderResult = tenderResultService.getById(id);
		 if(tenderResult==null) {
			 return Result.error("未找到对应数据");
		 }
		 // 2.得到开标记录中的投标申请id
		 //   根据投标申请id得到投标申记录
		 BusTenderRecord busTenderRecord = busTenderRecordService.getById(tenderResult.getBidRecordId());
		 // 1.根据投标结果得到开标记录id,再根据开标记录id得到投标申请id
		 String tenderApplicationId = busTenderRecordService.queryByRecordId(tenderResult.getBidRecordId());
		 // 2.根据投标申请id得到项目清单
		 QueryWrapper<BusTenderApplicationProInfTrack> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("tender_application_id",tenderApplicationId);
		 List<BusTenderApplicationProInfTrack> busTenderApplicationProInfTrackList = busTenderApplicationProInfTrackService.list(queryWrapper);
		 // 3.结果封装
		 BusTenderResultVO busTenderResultVO = new BusTenderResultVO();
		 busTenderResultVO.setTenderResult(tenderResult);
		 busTenderResultVO.setBusTenderApplicationProInfTrackList(busTenderApplicationProInfTrackList);
		 busTenderResultVO.setRecApplyTitle(busTenderRecord.getApplyTitle());
		 return Result.OK(busTenderResultVO);
	 }

    /**
    * 导出excel
    *
    * @param request
    * @param tenderResult
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TenderResult tenderResult) {
        return super.exportXls(request, tenderResult, TenderResult.class, "商务管理（投标结果）");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TenderResult.class);
    }

}
